package com.rdc.qingkou.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.contract.UserInfoContract;
import com.rdc.qingkou.entity.ModifyUserInfoResult;
import com.rdc.qingkou.presenter.UserInfoPresenter;
import com.rdc.qingkou.utils.ACache;

import java.io.File;

public class SculpturePreviewActivity extends AppCompatActivity implements UserInfoContract.IView{

    private ImageView sculpture_preview_imageView;
    private ImageButton sculpture_preview_imageButton;
    private Bitmap bitmap;

    private ACache aCache;

    private UserInfoPresenter mUserInfoPresenter;

    private File outputImage;

    @Override
    protected void onResume() {
        super.onResume();
        if (aCache.getAsBitmap("bitmap")!=null){
            Log.d("SculpturePreActivity","BITMAP IS NOT NULL");
            sculpture_preview_imageView.setImageBitmap(aCache.getAsBitmap("bitmap"));
            sculpture_preview_imageButton.setVisibility(View.VISIBLE);
            aCache.remove("bitmap");
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
        setContentView(R.layout.activity_sculpture_preview);

        mUserInfoPresenter = new UserInfoPresenter(this);

        aCache = ACache.get(this);

        sculpture_preview_imageView = (ImageView) findViewById(R.id.sculpture_preview_imageView);

        sculpture_preview_imageView.setImageDrawable(aCache.getAsDrawable("drawable"));  //good
        if (aCache.getAsBitmap("bitmap")!=null){
            sculpture_preview_imageView.setImageBitmap(aCache.getAsBitmap("bitmap"));
        }

        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.btn_choose).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SculpturePreviewActivity.this,UserInfoChooseDialogActivity.class));
//                //RETRY:
//                startActivityForResult(new Intent(SculpturePreviewActivity.this,ChooseImageDialogActivity.class),sCHOOSEMEDIA);
            }
        });
        sculpture_preview_imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return false;
            }
        });
        sculpture_preview_imageButton = (ImageButton)findViewById(R.id.sculpture_preview_imageButton);
        sculpture_preview_imageButton.setVisibility(View.GONE);
        sculpture_preview_imageButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.d("SculpturePreActivity",aCache.getAsString("gender"));  //0

                if (aCache.getAsObject("sculptureMode").equals(0)){
                    Log.d("SculpturePreActivity","sculptureMode IS 0");
                    outputImage = new File(Environment.getExternalStorageDirectory(),"output_image.jpg");

                    Log.d("SculpturePreActivity","LENGTH OF FILE IS : " + outputImage.length());

                    mUserInfoPresenter.modifyUserInfo(outputImage,
//                            Integer.getInteger(aCache.getAsString("gender")),   //java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
                            Integer.parseInt(aCache.getAsString("gender")),
                            aCache.getAsString("nickName"),
                            aCache.getAsString("signature"),
                            MyApplication.getUser().getUserId());
                }else{
                    Log.d("SculpturePreActivity","sculptureMode IS 1");
                    outputImage = new File(Environment.getExternalStorageDirectory(),"temp.jpg");
                    mUserInfoPresenter.modifyUserInfo(outputImage,
//                            Integer.getInteger(aCache.getAsString("gender")),
                            Integer.parseInt(aCache.getAsString("gender")),
                            aCache.getAsString("nickName"),
                            aCache.getAsString("signature"),
                            MyApplication.getUser().getUserId());
                }
            }
        });
    }

    @Override
    public void showData(ModifyUserInfoResult modifyUserInfoResult) {

    }

    @Override
    public void showModifyResult(ModifyUserInfoResult modifyUserInfoResult) {

        Log.d("SculpturePreActivity","SHOW MODIFY RESULT : iamgeUrl is" + modifyUserInfoResult.getUser().getImageUrl() +
                "  nickName is :"+ modifyUserInfoResult.getUser().getNickName());
        Toast.makeText(this,"更改图片成功！",Toast.LENGTH_SHORT).show();
        finish();

    }

    @Override
    public void showInfo(String info) {

        Log.d("SculpturePreActivity","ERROR :" + info);

    }
}
